import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Clerk, ClerkStatus } from 'src/core/entities/clerk.entity';
import { Repository } from 'typeorm';
import { UpdateClerkProfileDto } from './agent.dto';
import { Order } from 'src/core/entities/order.entity';

@Injectable()
export class AgentService {
  constructor(
    @InjectRepository(Clerk)
    private readonly clerkRepository: Repository<Clerk>,
    @InjectRepository(Order)
    private readonly orderRepository: Repository<Order>,
  ) {}

  getClerkProfile(id: number): Promise<Clerk | null> {
    return this.clerkRepository.findOne({
      where: { id },
      select: ['id', 'phone', 'avatar', 'nickname'],
    });
  }
  switchClerkStatus(id: number, status: ClerkStatus) {
    return this.clerkRepository.update(id, {
      status,
    });
  }

  setClerkProfile(id: number, data: UpdateClerkProfileDto) {
    return this.clerkRepository.update(id, data);
  }

  findUserOrderList(userId: number) {
    return this.orderRepository.findAndCount({
      where: {
        userId,
      },
    });
  }

  findUserOrderDetail(orderId: number) {
    return this.orderRepository.findOne({
      where: {
        id: orderId,
      },
    });
  }
}
